package cn.wit.summit.chwan.widgets.rhythm;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.DailyDiscoverResponse;
import cn.wit.summit.chwan.utils.MyImageLoader;

public class RhythmAdapter extends BaseAdapter {

    /**
     * item的宽度
     */
    private float itemWidth;
    /**
     * 数据源
     */
    private List<DailyDiscoverResponse> mCardList;

    private LayoutInflater mInflater;
    private Context mContext;
    private RhythmLayout mRhythmLayout;
    int rhythm_item_height;
    int rhythm_icon_margin;
    int rhythm_item_padding;

    public RhythmAdapter(Context context, RhythmLayout rhythmLayout, List<DailyDiscoverResponse> cardList) {
        this.mContext = context;
        this.mRhythmLayout = rhythmLayout;
        this.mCardList = new ArrayList();
        this.mCardList.addAll(cardList);
        rhythm_item_height = mContext.getResources().getDimensionPixelSize(R.dimen.dimen_dp_100);
        rhythm_icon_margin = mContext.getResources().getDimensionPixelSize(R.dimen.dimen_dp_1);
        rhythm_item_padding = (int) TypedValue.applyDimension(1, 8.0F, this.mContext.getResources().getDisplayMetrics());
        if (context != null)
            this.mInflater = LayoutInflater.from(context);
    }

    public List<DailyDiscoverResponse> getCardList() {
        return this.mCardList;
    }

    public void addCardList(List<DailyDiscoverResponse> cardList) {
        mCardList.addAll(cardList);
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.mCardList.size();
    }

    public DailyDiscoverResponse getItem(int position) {
        return this.mCardList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout relativeLayout = (RelativeLayout) this.mInflater.inflate(R.layout.adapter_rhythm_icon, null);

        //设置item布局的大小以及Y轴的位置
        relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams((int) itemWidth, rhythm_item_height ));
        relativeLayout.setTranslationY(itemWidth);
        relativeLayout.setPadding(0, rhythm_item_padding, 0, 0);

        //设置第二层RelativeLayout布局的宽和高
        RelativeLayout childRelativeLayout = (RelativeLayout) relativeLayout.getChildAt(0);
        int relativeLayoutWidth = (int) itemWidth - rhythm_icon_margin;
        childRelativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(relativeLayoutWidth, rhythm_item_height - rhythm_icon_margin));

        SimpleDraweeView imageIcon = (SimpleDraweeView) relativeLayout.findViewById(R.id.image_icon);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(relativeLayoutWidth, relativeLayoutWidth);
        params.setMargins(2 * rhythm_icon_margin,
                2 * rhythm_icon_margin,
                2 * rhythm_icon_margin, 0);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL | RelativeLayout.ALIGN_PARENT_TOP);
        imageIcon.setLayoutParams(params);

        if (mContext != null) {
            Log.i("Url", mCardList.get(position).getGame_ico());
//            MyImageLoader.load(mContext, mCardList.get(position).getIco_remote(), R.drawable.main_normal_icon,new GlideRoundTransform(this.mContext, 2),imageIcon);
            MyImageLoader.load(imageIcon, R.drawable.main_normal_icon, mCardList.get(position).getGame_ico(),MyImageLoader.makeRoundParams(mContext,2));
        }
        return relativeLayout;
    }


    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        this.mRhythmLayout.invalidateData();
    }

    public void setCardList(List<DailyDiscoverResponse> paramList) {
        this.mCardList = paramList;
    }

    /**
     * 设置每个item的宽度
     */
    public void setItemWidth(float width) {
        this.itemWidth = width;
    }
}